home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / internet-tools / wwwcache2.0 / rexx / cache.www next >
Encoding:
Text File  |  1996-02-26  |  15.3 KB  |  648 lines

  1. /*  $VER: Cache.www 2.0 (11.12.95) Stephan Lichtendahl
  2. Caching of inlined images and HTML documents on harddisk */
  3.  
  4. cachedir='AmiTCP:Cache/'
  5.  
  6. if ~show(l,'rexxsupport.library') then
  7.   if ~addlib('rexxsupport.library',0,-30,0) then
  8.     exit
  9. if ~show(l,'rexxdossupport.library') then
  10.   if ~addlib('rexxdossupport.library',0,-30,2) then
  11.     exit                                           .
  12.  
  13. hit=0
  14. zoek=0
  15. nl='0a'x
  16. cr='0d'x
  17. Update=''
  18. Port='AMOSAIC.1'
  19. OPTIONS RESULTS
  20.  
  21. parse arg args
  22. if ~ReadArgs(args,'Save/S,Load/S,Remove/S,Port,Update/K') | Port = '?' then do
  23.   say 'Usage: Cache.www [Save] to get/put images in current AMosaic'
  24.   say 'document from/in cache, Save will save HTML document as well.'
  25.   say 'Remove will remove the current AMosaic document from cache,'
  26.   say 'and every image in it which is currently visable (ie. loaded).'
  27.   say 'Cache.www Load reloads the current cached document from WWW.'
  28.   say 'Cache.www Update <AMosaic program> will update the hotlists'
  29.   say 'env(arc):mosaic/.mosaic-hotlist-default and start AMosaic.'
  30.   return 0
  31. end
  32. if Update ~= '' then do
  33.   if exists(cachedir'DoHTMLlist') then
  34.     call UpdateHL
  35.   address command 'Run >Nil: 'Update
  36.   return 0
  37. end
  38. address value Port
  39.  
  40. if ~Load & ~Remove then do
  41.   'fetch images IMAGE'
  42.  
  43.   call open(1input,cachedir'ImageList','R')
  44.   list = nl||readch(1input,65534)
  45.   call close(1input)
  46.  
  47.   do i=1 for image.0
  48.     urlnaam=image.i.url
  49.     av=image.i.current
  50.     nieuw.i=''
  51.  
  52.     if left(urlnaam,17) = 'file://localhost/' then do
  53.       if ~av then
  54.         'fetch inline url 'urlnaam
  55.      end
  56.     else do
  57.       uremote = substr(urlnaam,pos('://',urlnaam)+3)
  58.       call MkLocal
  59.       nr = pos(nl||uremote||nl,list)
  60.  
  61.       if nr = 0 & av then do
  62.         address command 'Echo >>'cachedir'ImageList "'uremote'"'
  63.         list=list||uremote||nl
  64.         call ChkTree
  65.       end
  66.       if nr > 0 & ~av then do
  67.         nieuw.i=ulocal
  68.         hit=2
  69.       end
  70.       if av then do
  71.         nieuw.i=ulocal
  72.         call open(2input,image.i.filename,'R')
  73.         call open(2output,cachedir||ulocal,'W')
  74.         do until eof(2input)
  75.           call writech(2output,readch(2input,65535))
  76.         end
  77.         call close(2input)
  78.         call close(2output)
  79.       end
  80.     end
  81.  
  82.   end
  83. end
  84.  
  85. 'get url'
  86. urlnaam = result
  87. if left(urlnaam,17) = 'file://localhost/' then do
  88.   ulocal = substr(urlnaam,18)
  89.   if left(ulocal,10) = 'T:temphtml' then
  90.     uremote=0
  91.   else
  92.     uremote=''
  93.  end
  94. else do
  95.   uremote = substr(urlnaam,pos('://',urlnaam)+3)
  96.   call MkLocal
  97. end
  98.  
  99. if uremote = 0 & (Load | Save) then do
  100.   'jump back'
  101.   return 0
  102. end
  103.  
  104. call open(3input,cachedir'HTMLlist','R')
  105. list = nl||readch(3input,65534)
  106. call close(3input)
  107.  
  108. if uremote = '' & Load then do
  109.   call MkRemote
  110.   if uremote ~= '' then
  111.     'jump url http://'uremote
  112. end
  113.  
  114. if Save then do
  115.   if uremote = '' then do
  116.     call MkRemote
  117.     if uremote = '' then do
  118.       address command 'GetString "Local document - change URL for remote links" String "http://'ulocal'" Var newurl Global'
  119.       if RC ~= 0 then
  120.         return 0
  121.       else do
  122.         urlnaam = getvar('newurl')
  123.         call deletevar('newurl')
  124.         uremote = substr(urlnaam,pos('://',urlnaam)+3)
  125.         call MkLocal
  126.       end
  127.     end
  128.   end
  129.  
  130.   hit=hit+1
  131.   saveto=cachedir||ulocal
  132.  
  133.   nr = pos(nl||uremote||nl,list)
  134.   if nr = 0 then do
  135.     address command 'Echo >>'cachedir'HTMLlist "'uremote'"'
  136.     list=list||uremote||nl
  137.     call ChkTree
  138.     'fetch title'
  139.     titel = compress(result,nl'"'cr)
  140.     address command 'Echo >>'cachedir'Cache.html "<A HREF=*"/'saveto'*">'titel'</A><BR>"'
  141.     address command 'Echo >>'cachedir'DoHTMLlist "+'uremote'"'
  142.  
  143.     zoek=1
  144.     zoek.0=urlnaam
  145.     vervang.0='/'saveto
  146.   end
  147. end
  148.  
  149. if Remove then do
  150.   'fetch images IMAGE'
  151.  
  152.   if uremote ~= 0 then do
  153.     IsHTML=1
  154.     if uremote = '' then
  155.       call MkRemote
  156.     if uremote ~= '' then
  157.       call Verwijder
  158.   end
  159.  
  160.   IsHTML=0
  161.   q=0
  162.   call open(1input,cachedir'ImageList','R')
  163.   list = nl||readch(1input,65534)
  164.   call close(1input)
  165.  
  166.   do i=1 for image.0
  167.     if image.i.current then do
  168.       urlnaam=image.i.url
  169.       if left(urlnaam,17) = 'file://localhost/' then do
  170.         ulocal = substr(urlnaam,18)
  171.         call MkRemote
  172.        end
  173.       else do
  174.         uremote = substr(urlnaam,pos('://',urlnaam)+3)
  175.         call MkLocal
  176.       end
  177.       call Verwijder
  178.     end
  179.   end
  180.  
  181.   if q then do
  182.     call open(1output,cachedir'Imagelist','W')
  183.     call writech(1output,substr(list,2))
  184.     call close(1output)
  185.   end
  186.  
  187.   if zoek > 0 then
  188.     call MultiFR
  189. end
  190.  
  191. if hit>0 then do
  192.   if hit = 2 then
  193.     do n=1 until ~exists(saveto)
  194.       saveto='T:temphtml'n
  195.     end
  196.   urlstam = left(urlnaam,lastpos('/',urlnaam)-1)
  197.   urlroot = left(urlnaam,pos('/',urlnaam,8)-1)
  198.  
  199.   'fetch source'
  200.   inhoud = result
  201.   nr = max(pos('<html',inhoud),pos('<HTML',inhoud))
  202.   if nr > 0 then do
  203.     startnr=nr+5
  204.     stopnr = pos('>',inhoud,startnr)
  205.     if startnr ~= stopnr then
  206.       inhoud = delstr(inhoud,startnr,stopnr-startnr)
  207.   end
  208.   if nr > 1 then
  209.     inhoud = substr(inhoud,nr)
  210.  
  211.   call ChkLinks
  212.   zkinhoud=''
  213.   info='<A HREF="mailto:s.e.m.lichtendahl@et.tudelft.nl">WWWCache 2.0 © S.Lichtendahl</A>'
  214.   call ChkCache
  215.   if hit=2 then
  216.     inhoud=inhoud'<H6>This is a temporal page created by 'info'</H6>'
  217.  
  218.   call open(4output,saveto,'W')
  219.   call writech(4output,inhoud)
  220.   call close(4output)
  221.   inhoud=''
  222.  
  223.   'jump url file://localhost/'saveto
  224.   do i=1 for image.0
  225.     if nieuw.i ~= '' then
  226.       'fetch inline url /'cachedir||nieuw.i
  227.   end
  228.   if hit = 2 then
  229.     call delete(saveto)
  230.   if zoek > 0 then
  231.     call MultiFR
  232.  
  233. end
  234. exit 0
  235.  
  236. MkLocal:
  237.   zoeknr = pos(':',uremote)
  238.   if zoeknr > 0 then
  239.     if left(uremote,10) = 'localhost/' then
  240.       ulocal = substr(uremote,zoeknr+1)
  241.     else do
  242.       midnr = pos('/',uremote,zoeknr+1)
  243.       ulocal = delstr(uremote,zoeknr,midnr-zoeknr)
  244.     end
  245.   else
  246.     ulocal=uremote
  247.   if right(ulocal,1) = '/' then
  248.     ulocal=ulocal'.html'
  249. return 0
  250.  
  251. MkRemote:
  252.   if left(ulocal,length(cachedir)) ~= cachedir then
  253.     uremote=''
  254.   else do
  255.     ulocal = substr(ulocal,length(cachedir)+1)
  256.     if right(ulocal,6) = '/.html' then
  257.       uremote = left(ulocal,length(ulocal)-5)
  258.     else
  259.       uremote=ulocal
  260.  
  261.     if pos(nl||uremote||nl,list) = 0 then do
  262.       stopnr=0
  263.       nr = pos(':'uremote||nl,list)
  264.       if nr > 0 then do
  265.         startnr = lastpos(nl,list,nr)+1
  266.         stopnr = pos(nl,list,nr)
  267.  
  268.        end
  269.       else do
  270.         nr = pos('/',uremote)
  271.         uleft = left(uremote,nr-1)':'
  272.         uright = substr(uremote,nr)nl
  273.  
  274.         nr = pos(uright,list)
  275.         do while nr > 0 & stopnr = 0
  276.           startnr = lastpos(nl,list,nr)+1
  277.           zoeknr = pos(nl,list,nr)
  278.           if substr(list,startnr,length(uleft)) = uleft then
  279.             stopnr=zoeknr
  280.  
  281.           nr = pos(uright,list,zoeknr+1)
  282.         end
  283.       end
  284.  
  285.       if stopnr > 0 then 
  286.         uremote = substr(list,startnr,stopnr-startnr)
  287.       else
  288.         uremote=''
  289.     end
  290.  
  291.   end
  292. return 0
  293.  
  294. ChkTree:
  295.   zoeknr = pos('/',ulocal)
  296.   do while zoeknr > 0
  297.     pad = cachedir||left(ulocal,zoeknr-1)
  298.     if ~exists(pad) then
  299.       call makedir(pad)
  300.  
  301.     zoeknr = pos('/',ulocal,zoeknr+1)
  302.   end
  303. return 0
  304.  
  305. ChkLinks:
  306.   delta=0
  307.   zkinhoud = upper(inhoud)
  308.   zoeknr = pos('<A',zkinhoud)
  309.   do while zoeknr > 0
  310.  
  311.     zoeknr = verify(zkinhoud,nl cr,,zoeknr+2)
  312.     if substr(zkinhoud,zoeknr,4) ~= 'HREF' then
  313.       zklen=1
  314.     else do
  315.       zoeknr = verify(zkinhoud,nl cr,,zoeknr+4)
  316.       if substr(zkinhoud,zoeknr,1) ~= '=' then
  317.         zklen=1
  318.       else do
  319.         zoeknr = verify(zkinhoud,nl cr,,zoeknr+1)
  320.  
  321.         if substr(zkinhoud,zoeknr,1) = '"' then
  322.           zoeknr=zoeknr+1
  323.         else do
  324.           inhoud = insert('"',inhoud,zoeknr+delta-1)
  325.           delta=delta+1
  326.         end
  327.         stopnr = pos('>',zkinhoud,zoeknr)
  328.         zklen=stopnr-zoeknr
  329.  
  330.         urlnaam = substr(zkinhoud,zoeknr,zklen)
  331.         if pos('://',urlnaam) = 0 then
  332.           call ReplUrl
  333.         if right(urlnaam,1) ~= '"' then do
  334.           inhoud = insert('"',inhoud,stopnr+delta-1)
  335.           delta=delta+1
  336.         end
  337.  
  338.       end
  339.     end
  340.     zoeknr = pos('<A',zkinhoud,zoeknr+zklen)
  341.   end
  342.  
  343.   delta=0
  344.   zkinhoud = upper(inhoud)
  345.   zoeknr = pos('SRC',zkinhoud)
  346.   do while zoeknr > 0
  347.  
  348.     nr = verify(zkinhoud,nl cr,'M',zoeknr-1)
  349.     if nr ~= zoeknr-1 then
  350.       zklen=1
  351.     else do
  352.       zoeknr = verify(zkinhoud,nl cr,,zoeknr+3)
  353.       if substr(zkinhoud,zoeknr,1) ~= '=' then
  354.         zklen=1
  355.       else do
  356.         zoeknr = verify(zkinhoud,nl cr,,zoeknr+1)
  357.  
  358.         if substr(zkinhoud,zoeknr,1) = '"' then
  359.           zoeknr=zoeknr+1
  360.         else do
  361.           inhoud = insert('"',inhoud,zoeknr+delta-1)
  362.           delta=delta+1
  363.         end
  364.         stopnr = verify(zkinhoud,'">'nl cr,'M',zoeknr)+1
  365.         zklen=stopnr-zoeknr
  366.  
  367.         urlnaam = substr(zkinhoud,zoeknr,zklen)
  368.         if pos('://',urlnaam) = 0 then
  369.           call ReplUrl
  370.         if right(urlnaam,1) ~= '"' then do
  371.           inhoud = insert('"',inhoud,stopnr+delta-2)
  372.           delta=delta+1
  373.         end
  374.  
  375.       end
  376.     end
  377.     zoeknr = pos('SRC',zkinhoud,zoeknr+zklen)
  378.   end
  379.  
  380.   delta=0
  381.   zkinhoud = upper(inhoud)
  382.   zoeknr = pos('ACTION',zkinhoud)
  383.   do while zoeknr > 0
  384.  
  385.     zoeknr = verify(zkinhoud,nl cr,,zoeknr+6)
  386.     if substr(zkinhoud,zoeknr,1) ~= '=' then
  387.       zklen=1
  388.     else do
  389.       zoeknr = verify(zkinhoud,'"'nl cr,,zoeknr+1)
  390.       stopnr = pos('"',zkinhoud,zoeknr)
  391.       zklen=stopnr-zoeknr
  392.  
  393.       urlnaam = substr(zkinhoud,zoeknr,zklen)
  394.       if pos('://',urlnaam) = 0 then
  395.         call ReplUrl
  396.     end
  397.     zoeknr = pos('ACTION',zkinhoud,zoeknr+zklen)
  398.   end
  399. return 0
  400.  
  401. ReplUrl:
  402.   nr = pos(':',urlnaam)
  403.   if nr>0 then
  404.     if left(urlnaam,5) = 'HTTP:' then do
  405.       urlnaam = substr(urlnaam,6)
  406.       inhoud = delstr(inhoud,zoeknr+delta,5)
  407.       zoeknr=zoeknr+5
  408.       delta=delta-5
  409.       nr=0
  410.     end
  411.  
  412.   if nr=0 then
  413.     if left(urlnaam,1) ~= '#' then do
  414.       if left(urlnaam,1) = '/' then
  415.         tekst=urlroot
  416.       else do
  417.         tekst=urlstam
  418.  
  419.         do while left(urlnaam,3) = '../'
  420.           urlnaam = substr(urlnaam,4)
  421.           inhoud = delstr(inhoud,zoeknr+delta,3)
  422.           zoeknr=zoeknr+3
  423.           delta=delta-3
  424.           tekst = left(tekst,lastpos('/',tekst)-1)
  425.         end
  426.  
  427.         tekst=tekst'/'
  428.       end
  429.  
  430.       inhoud = insert(tekst,inhoud,zoeknr+delta-1)
  431.       delta=delta+length(tekst)
  432.     end
  433. return 0
  434.  
  435. ChkCache:
  436.   do i=1 for image.0
  437.     if nieuw.i ~= '' then do
  438.       nr = pos(image.i.url'"',inhoud)
  439.       if nr = 0 then
  440.         inhoud=inhoud'<H6>Error: Image URL 'image.i.url' not found by 'info' Please mail URL of this page</H6>'
  441.       else do
  442.  
  443.         inhoud = delstr(inhoud,nr,length(image.i.url))
  444.         inhoud = insert('/'cachedir||nieuw.i,inhoud,nr-1)
  445.       end
  446.     end
  447.   end
  448.  
  449.   if hit ~= 2 then do
  450.     'fetch links HREF'
  451.     do i=1 for href.0
  452.       urlnaam=href.i.url
  453.       if left(urlnaam,17) ~= 'file://localhost/' then do
  454.         uremote = substr(urlnaam,pos('://',urlnaam)+3)
  455.         nr = pos(nl||uremote||nl,list)
  456.         if nr > 0 then do
  457.  
  458.           call MkLocal
  459.           nr = pos(urlnaam,inhoud)
  460.           do while nr > 0
  461.             zklen=length(urlnaam)
  462.             tekst=substr(inhoud,nr+zklen,1)
  463.  
  464.             if tekst='#' | tekst='"' then
  465.               if pos('ACTION',upper(substr(inhoud,nr-11,10))) = 0 then do
  466.  
  467.                 inhoud = delstr(inhoud,nr,zklen)
  468.                 inhoud = insert('/'cachedir||ulocal,inhoud,nr-1)
  469.               end
  470.  
  471.             nr = pos(urlnaam,inhoud,nr+zklen)
  472.           end
  473.  
  474.         end
  475.       end
  476.     end
  477.  
  478.   end
  479. return 0
  480.  
  481. Verwijder:
  482.   nr = pos(nl||uremote||nl,list)
  483.   if nr > 0 then do
  484.     list = delstr(list,nr+1,length(uremote)+1)
  485.     call delete(cachedir||ulocal)
  486.     say cachedir||ulocal 'deleted.'
  487.  
  488.     if IsHTML then do
  489.       call open(3output,cachedir'HTMLlist','W')
  490.       call writech(3output,substr(list,2))
  491.       call close(3output)
  492.  
  493.       call open(5input,cachedir'Cache.html','R')
  494.       list = readch(5input,65535)
  495.       call close(5input)
  496.  
  497.       nr = pos('/'cachedir||ulocal'"',list)
  498.       if nr > 0 then do
  499.         startnr = lastpos(nl,list,nr)+1
  500.         stopnr = pos(nl,list,nr)+1
  501.         list = delstr(list,startnr,stopnr-startnr)
  502.       end
  503.  
  504.       call open(5output,cachedir'Cache.html','W')
  505.       call writech(5output,list)
  506.       call close(5output)
  507.  
  508.       address command 'Echo >>'cachedir'DoHTMLlist "-'uremote'"'
  509.     end
  510.  
  511.     q=1
  512.     zoek.zoek='/'cachedir||ulocal
  513.     vervang.zoek='http://'uremote
  514.     zoek=zoek+1
  515.   end
  516. return 0
  517.  
  518. MultiFR:
  519.   call open(3input,cachedir'HTMLlist','R')
  520.   list = readch(3input,65535)
  521.   call close(3input)
  522.  
  523.   startnr=1
  524.   stopnr = pos(nl,list)
  525.   do while stopnr > 0
  526.     uremote = substr(list,startnr,stopnr-startnr)
  527.     call MkLocal
  528.  
  529.     q=0
  530.     call open(6input,cachedir||ulocal,'R')
  531.     inhoud = readch(6input,65535)
  532.     call close(6input)
  533.  
  534.     do i=0 for zoek
  535.       nr = pos(zoek.i,inhoud)
  536.       do while nr > 0
  537.         zklen=length(zoek.i)
  538.         tekst=substr(inhoud,nr+zklen,1)
  539.  
  540.         if tekst='#' | tekst='"' then
  541.           if pos('ACTION',upper(substr(inhoud,nr-11,10))) = 0 then do
  542.             q=1
  543.             inhoud = delstr(inhoud,nr,zklen)
  544.             inhoud = insert(vervang.i,inhoud,nr-1)
  545.             zklen=length(vervang.i)
  546.           end
  547.  
  548.         nr = pos(zoek.i,inhoud,nr+zklen)
  549.       end
  550.     end                                                         
  551.  
  552.     if q then do
  553.       call open(6output,cachedir||ulocal,'W')
  554.       call writech(6output,inhoud)
  555.       call close(6output)
  556.       say cachedir||ulocal 'updated.'
  557.     end
  558.  
  559.     startnr=stopnr+1
  560.     stopnr = pos(nl,list,startnr)
  561.   end
  562. return 0
  563.  
  564. UpdateHL:
  565.   call open(3input,cachedir'DoHTMLlist','R')
  566.   list = readch(3input,65535)
  567.   call close(3input)
  568.  
  569.   call open(7input,'Env:Mosaic/.mosaic-hotlist-default','R')
  570.   inhoud = readch(7input,65535)
  571.   call close(7input)
  572.  
  573.   cachtml=''
  574.   startnr=1
  575.   stopnr = pos(nl,list)
  576.   do while stopnr > 0
  577.     tekst = substr(list,startnr,1)
  578.     uremote = substr(list,startnr+1,stopnr-startnr-1)
  579.     call MkLocal
  580.  
  581.     if tekst = '-' then do
  582.       zoeknr = pos('file://localhost/'cachedir||ulocal,inhoud)
  583.       if zoeknr > 0 then do
  584.         nr = pos(nl,inhoud,zoeknr+17)+2
  585.         inhoud = delstr(inhoud,zoeknr,nr-zoeknr)
  586.         zoeknr=zoeknr-1
  587.         urlnaam='http://'uremote
  588.       end
  589.     end
  590.     if tekst = '+' then do
  591.       if pos('/'cachedir||ulocal,inhoud) then
  592.         zoeknr=0
  593.       else do
  594.         urlnaam='file://localhost/'cachedir||ulocal
  595.  
  596.         zoeknr = pos('http://'uremote,inhoud)
  597.         if zoeknr > 0 then do
  598.           nr = pos(nl,inhoud,zoeknr+7)+1
  599.           inhoud = delstr(inhoud,zoeknr,nr-zoeknr)
  600.           zoeknr=zoeknr-1
  601.           inhoud = insert('©',inhoud,zoeknr)
  602.  
  603.          end
  604.         else do
  605.           zoeknr = pos('Default',inhoud)+7
  606.  
  607.           if cachtml = '' then do
  608.             call open(5input,cachedir'Cache.html','R')
  609.             cachtml = readch(5input,65535)
  610.             call close(5input)
  611.           end
  612.  
  613.           tekst='/'cachedir||ulocal'">'
  614.           midnr = pos(tekst,cachtml)
  615.           if midnr = 0 then
  616.             titel = 'URL: http://'uremote
  617.           else do
  618.             midnr=midnr+length(tekst)
  619.             nr = pos('<',cachtml,midnr)
  620.             titel = substr(cachtml,midnr,nr-midnr)
  621.           end
  622.           inhoud = insert('©'titel||nl,inhoud,zoeknr)
  623.         end
  624.  
  625.       end
  626.     end
  627.  
  628.     if zoeknr > 0 then do
  629.       parse value date() with datum maand jaar .
  630.       dag = left(date('W'),3)
  631.       inhoud = insert(urlnaam dag maand datum time() jaar||nl,inhoud,zoeknr)
  632.     end
  633.  
  634.     startnr=stopnr+1
  635.     stopnr = pos(nl,list,startnr)
  636.   end
  637.  
  638.   call delete(cachedir'DoHTMLlist')
  639.  
  640.   call open(7output,'Env:Mosaic/.mosaic-hotlist-default','W')
  641.   call writech(7output,inhoud)
  642.   call close(7output)
  643.   call open(7output,'EnvArc:Mosaic/.mosaic-hotlist-default','W')
  644.   call writech(7output,inhoud)
  645.   call close(7output)
  646. return 0
  647.  
  648.